﻿using System.Runtime.InteropServices;
using System.Text;

namespace OrbitOne.Elmah.Logic
{
    enum COMPUTER_NAME_FORMAT
    {
        ComputerNameNetBIOS,
        ComputerNameDnsHostname,
        ComputerNameDnsDomain,
        ComputerNameDnsFullyQualified,
        ComputerNamePhysicalNetBIOS,
        ComputerNamePhysicalDnsHostname,
        ComputerNamePhysicalDnsDomain,
        ComputerNamePhysicalDnsFullyQualified
    }

    /// <summary>
    /// Represents a comwrapper. This is a collection of marshalled methods.
    /// </summary>
    public class ComWrapper
    {
        [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern bool GetComputerNameEx(COMPUTER_NAME_FORMAT NameType, [Out] StringBuilder lpBuffer, ref uint lpnSize);

        /// <summary>
        /// Gets the current computer's full dns name.
        /// </summary>
        /// <returns></returns>
        public static string ComputerFullDnsName()
        {
            var name = new StringBuilder();
            uint size = 255;
            GetComputerNameEx(COMPUTER_NAME_FORMAT.ComputerNameDnsFullyQualified, name, ref size);
            return name.ToString();
        }
    }
}